package com.xiaozhen.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.security.controller.BaseController;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.domain.system.SysRole;
import com.xiaozhen.common.core.domain.system.queryParams.SysRoleQueryParam;
import com.xiaozhen.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/8/18 11:58
 * @description:
 */
@Api(tags = "系统角色接口")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("获取角色列表")
    @GetMapping("/list")
    public AjaxResult list(SysRoleQueryParam queryParam) {
        Page<SysRole> page = startPage();
        Page<SysRole> list = sysRoleService.selectRoleList(page, queryParam);
        return getDataTable(list);
    }

    @ApiOperation("添加角色信息")
    @PostMapping("/addSysRole")
    public AjaxResult addSysRole(@Validated @RequestBody SysRole sysRole) {
        if (!sysRoleService.checkRoleNameUnique(sysRole)) {
            return AjaxResult.error("新增角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }
        if (!sysRoleService.checkRoleKeyUnique(sysRole)) {
            return AjaxResult.error("新增角色'" + sysRole.getRoleName() + "'失败，角色权限已存在");
        }
        sysRole.setCreateBy(getCurrentUserId());
        sysRole.setCreateTime(LocalDateTime.now());
        return toAjax(sysRoleService.insertSysRole(sysRole));
    }

    @ApiOperation("修改角色信息")
    @PutMapping("/updateSysRole")
    public AjaxResult updateSysRole(@Validated @RequestBody SysRole sysRole) {
        Boolean allowed = sysRoleService.checkRoleAllowed(sysRole);
        if (!allowed) {
            return AjaxResult.error("不允许操作超级管理员角色");
        }
        if (!sysRoleService.checkRoleNameUnique(sysRole)) {
            return AjaxResult.error("修改角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }
        if (!sysRoleService.checkRoleKeyUnique(sysRole)) {
            return AjaxResult.error("修改角色'" + sysRole.getRoleName() + "'失败，角色权限已存在");
        }
        sysRole.setUpdateBy(getCurrentUserId());
        sysRole.setUpdateTime(LocalDateTime.now());
        if (sysRoleService.updateSysRole(sysRole) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改角色'" + sysRole.getRoleName() + "'失败，请联系管理员");
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds) {
        return toAjax(sysRoleService.deleteRoleByIds(roleIds));
    }

    @ApiOperation("分配角色菜单权限")
    @PutMapping("/assignMenuPermission")
    public AjaxResult assignMenuPermission(@RequestBody SysRole sysRole) {
        Boolean allowed = sysRoleService.checkRoleAllowed(sysRole);
        if (!allowed) {
            return AjaxResult.error("不允许操作超级管理员角色");
        }
        sysRoleService.assignMenuPermission(sysRole.getRoleId(),sysRole.getMenuIds());
        return AjaxResult.success("配置角色菜单权限成功");
    }
}
